package m.member.manage.service;

import m.common.model.util.ModelQueryList;
import m.common.model.util.ModelUpdateUtil;
import m.common.model.util.QueryCondition;
import m.common.service.Service;
import m.member.manage.model.MemberInfo;
import m.member.manage.model.MoneyRecord;
import m.member.manage.service.inter.MoneyRecordInter;
import m.system.exception.MException;
import m.system.util.GenerateID;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class MoneyRecordService extends Service {

    public void add(String type,Class<? extends MoneyRecordInter> clazz,String busiOid,String memberOid, Double realMoney, Double giveMoney, Integer point) throws MException {
        MoneyRecord model=new MoneyRecord();
        model.setOid(GenerateID.generatePrimaryKey());
        model.setDate(new Date());
        model.setType(type);
        model.setBusiService(clazz.getName());
        model.setBusiOid(busiOid);
        model.setMember(new MemberInfo());
        model.getMember().setOid(memberOid);
        model.setRealMoney(realMoney);
        model.setGiveMoney(giveMoney);
        model.setPoint(point);
        ModelUpdateUtil.insertModel(model);
    }
    public void remove(Class<? extends MoneyRecordInter> clazz,String busiOid) throws MException, SQLException {
        List<MoneyRecord> list= ModelQueryList.getModelList(MoneyRecord.class,new String[]{"oid"},null,
            QueryCondition.and(
                QueryCondition.eq("busiService",clazz.getName()),
                QueryCondition.eq("busiOid",busiOid)
            )
        );
        if(list.size()>0){
            ModelUpdateUtil.deleteModels(list.toArray(new MoneyRecord[0]));
        }
    }
}
